home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / qlib205.zip / QLIB.ZIP / C / PACKIT.C < prev    next >
C/C++ Source or Header  |  1997-02-09  |  3KB  |  159 lines

  1. /* PACKIT v2.10
  2.  
  3. v2.10 - can output header to another file (see pack.txt)
  4. v2.01 - will no longer add fileout to itself if it already exists
  5. v2.00 - 32bit version...
  6. v1.00 - simple 16bit version...
  7.  
  8. */
  9.  
  10. #include <qlib.h>
  11. #include <dos.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. byte fixer;
  17.  
  18. struct bufs{
  19.   byte fn[13];  //0-12
  20.   word fh;      //always 0
  21.   dword off;    //offset
  22.   dword siz;    //size of file
  23. } sample;
  24.  
  25. struct bufs *buf;
  26.  
  27. dword coff;  //current offset
  28. word cbuf;
  29. dword siz;
  30. int hi,ho,t;
  31. byte str[80];
  32. #define tbufsiz 16*1024
  33. byte tbuf[tbufsiz];
  34. dword a;
  35. word cnt;
  36. dword head1=0x1a4b4150;   //"PAK",26
  37. dword head2=0x1a484150;   //"PAH",26
  38. byte fo[80];
  39. byte fohdr[80];
  40. byte mode;  //0=normal 1=output header to other file
  41.  
  42. struct ffblk ff;
  43.  
  44. void main(byte argc,byte **args) {
  45.  
  46.   dword siz;
  47.  
  48.   if (argc<2 || argc>3) {
  49.     printf(" PACKIT v2.10 (32bit) by : Peter Quiring\n");
  50.     printf(" Usage: PACKIT fileout [header_fileout]\n");
  51.     exit(0);
  52.   }
  53.  
  54.   strcpy(fo,args[1]);
  55.   strupr(fo);
  56.   buf=(void*)malloc(sizeof(sample)*65000);
  57.   if ( ((dword)buf)==ERROR) {
  58.     printf("Out of ram...\n");
  59.     abort();
  60.   }
  61.  
  62.   if (argc==2) mode=0;
  63.   else {
  64.     mode=1;
  65.     strcpy(fohdr,args[2]);
  66.     strupr(fohdr);
  67.   }
  68.  
  69.   coff=0;    //current offset
  70.   cbuf=0;    //current buffer
  71.  
  72.   printf("Gathering file info...");
  73.   findfirst("*.*",FA_ARCH,&ff);
  74.   while (1) {
  75.     strcpy(str,ff.ff_name);
  76.     strupr(str);
  77.     if (!strcmp(str,fo)) {
  78.       if (findnext(&ff)) break;
  79.       continue;
  80.     }
  81.     if (!strcmp(str,fohdr)) {
  82.       if (findnext(&ff)) break;
  83.       continue;
  84.     }
  85.     t=open(str,O_BINARY);
  86.     if (t==-1) {
  87.       printf("\nUnable to open:%s",str);
  88.       exit(0);
  89.     }
  90.     siz=filelength(t);
  91.     memset(buf[cbuf].fn,0,13);
  92.     strcpy(buf[cbuf].fn,str);
  93.     buf[cbuf].fh=0;
  94.     buf[cbuf].off=coff;
  95.     buf[cbuf].siz=siz;
  96.     close(t);
  97.     cbuf++;
  98.     if (cbuf>=65000) {
  99.       printf("File limit: 65000 files");
  100.       abort();
  101.     }
  102.     coff+=siz;
  103.     if (findnext(&ff)) break;
  104.   }
  105.   printf("# files found=%d\n",cbuf);
  106.  
  107.   if (mode)
  108.     ho=open(fohdr,O_BINARY|O_CREAT|O_TRUNC|O_RDWR);
  109.   else
  110.     ho=open(fo,O_BINARY|O_CREAT|O_TRUNC|O_RDWR);
  111.  
  112.   if (ho==-1) {
  113.     printf("File I/O error"); exit(0);
  114.   }
  115.  
  116.   if (!mode) write(ho,&head1,4);
  117.  
  118.   write(ho,&cbuf,2);   //# of files (part of header)
  119.   buf[cbuf].fn[0]=0;
  120.   /* now create packfile output
  121.   format =
  122.     filename [13]
  123.     handle [2]  ;not used in file
  124.     offset [4]
  125.     size [4]
  126.   */
  127.   if (mode) a=4;
  128.   else a=6+cbuf*23;
  129.   cbuf=0;
  130.   while(buf[cbuf].fn[0]){
  131.     buf[cbuf].off+=a;
  132.     write(ho,&buf[cbuf],sizeof(buf[0]));
  133.     cbuf++;
  134.   }
  135.   if (mode) {
  136.     close(ho);
  137.     ho=open(fo,O_BINARY|O_CREAT|O_TRUNC|O_RDWR);
  138.     if (ho==-1) {
  139.       printf("File I/O error"); exit(0);
  140.     }
  141.     write(ho,&head2,4);
  142.   }
  143.   //output the actual files!
  144.   cbuf=0;
  145.   while(buf[cbuf].fn[0]){
  146.     hi=open(buf[cbuf].fn,O_BINARY|O_RDONLY);
  147.     while (1) {
  148.       a=read(hi,tbuf,tbufsiz);
  149.       if (!a) break;
  150.       write(ho,tbuf,a);
  151.     }
  152.     close(hi);
  153.     printf("%s - done!\n",buf[cbuf].fn);
  154.     cbuf++;
  155.   }
  156.   close(hi);
  157.   printf("\n all done!!\n");
  158. }
  159.